package com.cqndt.disaster.device.service.impl;

import java.util.Map;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.net.URLEncoder;
import java.io.OutputStream;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import com.cqndt.disaster.device.vo.SearchVo;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.springframework.stereotype.Service;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import com.cqndt.disaster.device.dao.TbsMapper;
import com.cqndt.disaster.device.domain.TabDevice;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import com.cqndt.disaster.device.dao.TabDeviceMapper;
import com.cqndt.disaster.device.dao.TabSensorMapper;
import com.cqndt.disaster.device.service.TabDeviceService;
import com.cqndt.disaster.device.service.TabDevicePatrolService;

@Service
public class TabDevicePatrolServiceImpl implements TabDevicePatrolService {

	@Resource
	private TbsMapper tbsMapper;
	@Resource
	private TabDeviceService tabDeviceService;
	@Resource
	private TabSensorMapper tabSensorMapper;
	@Resource
	private TabDeviceMapper tabDeviceMapper;

	@Override
	public void getEchartExport(SearchVo vo, 
				HttpServletRequest request,HttpServletResponse response) {
		String[] sensorNos = vo.getSensorNo().split(",");
		String sensorNo = sensorNos[0];
		String deviceNo = tabSensorMapper.selectDeviceNoBySensorNo(sensorNo);
		TabDevice tabDevice = tabDeviceMapper.getDevice(deviceNo);
		String monitorNo = tabSensorMapper.selectMonitorNoBySensorNo(sensorNo);
		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		SXSSFSheet sheet = sxssfWorkbook.createSheet("设备" + deviceNo);
		// 创建表头
		SXSSFRow headRow = sheet.createRow(0);
		Integer type = Integer.valueOf(tabDevice.getDeviceType());
		switch (type) {
		case 1:// 裂缝
			vo.setSensorNo(deviceNo);
			List<Map<String,Object>> data = tbsMapper.tbsLfAll(vo);
			String[] lxBt=new String[]{"数据上传时间","x加速度","y加速度","z加速度","x角度","y角度","z角度","拉线值(mm)"};
			for (int a = 0; a < lxBt.length; a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt[a]);
			}
			for (Map<String, Object> value : data) {
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
				dataRow.createCell(2).setCellValue(String.valueOf(value.get("param2")));
				dataRow.createCell(3).setCellValue(String.valueOf(value.get("param3")));
				dataRow.createCell(4).setCellValue(String.valueOf(value.get("param4")));
				dataRow.createCell(5).setCellValue(String.valueOf(value.get("param5")));
				dataRow.createCell(6).setCellValue(String.valueOf(value.get("param6")));
				dataRow.createCell(7).setCellValue(String.valueOf(value.get("param7")));
			}
			break;
		case 4:
			// 地面倾斜
			vo.setSensorNo(monitorNo);
			List<Map<String, Object>> data1 = tbsMapper.tbsDbwy(vo);
			String[] lxBt1 = new String[] { "数据上传时间", "北向变形", "东向变形", "高向变形" };
			for (int a = 0; a < lxBt1.length; a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt1[a]);
			}
			for (Map<String, Object> value : data1) {
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
				dataRow.createCell(2).setCellValue(String.valueOf(value.get("param2")));
				dataRow.createCell(3).setCellValue(String.valueOf(value.get("param3")));
			}
			break;
		case 5 :
			vo.setSensorNo(deviceNo);
			List<Map<String,Object>> data5 = tbsMapper.tbsSbwyTimeList1(vo); 
			if(!data5.isEmpty()) {
				for(Map<String,Object> map : data5) {
					String times = map.get("time").toString();
					List<Map<String,Object>> data5y=tbsMapper.tbsSbwyTimeList2(deviceNo,times);
					map.put("param2", data5y.get(0).toString());
				}
			}
			String[] lxBt5 = new String[]{"数据上传时间","x偏移量","y偏移量"};
			for (int a = 0;a < lxBt5.length;a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt5[a]);
			}
			for (Map<String,Object> value : data5){
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
				dataRow.createCell(2).setCellValue(String.valueOf(value.get("param2")));
			}
			break;
		case 30:
			vo.setSensorNo(deviceNo);
			List<Map<String,Object>> data30 = tbsMapper.tbsDxs(vo);
			String[] lxBt30=new String[]{"数据上传时间","水位高度"};
			for (int a=0;a<lxBt30.length;a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt30[a]);
			}
			for (Map<String,Object> value : data30){
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
			}
			break;
		case 50:
			// 地面倾斜
			vo.setSensorNo(sensorNo);
			List<Map<String, Object>> data2 = tbsMapper.tbsDmqxjcy(vo);
			String[] lxBt2 = new String[] { "数据上传时间", "主向位移(mm)", "侧向位移(mm)", "沉降位移(mm)" };
			for (int a = 0; a < lxBt2.length; a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt2[a]);
			}
			for (Map<String, Object> value : data2) {
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
				dataRow.createCell(2).setCellValue(String.valueOf(value.get("param2")));
				dataRow.createCell(3).setCellValue(String.valueOf(value.get("param3")));
			}
			break;
		case 51:
			// 地裂缝、墙裂缝
			vo.setSensorNo(sensorNo);
			List<Map<String, Object>> data3 = tbsMapper.tbsDlfQlf(vo);
			String[] lxBt3 = new String[] { "数据上传时间", "x倾角(°)", "y倾角(°)", "z倾角(°)", "拉线(mm)", "相对位移量(mm)" };
			for (int a = 0; a < lxBt3.length; a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt3[a]);
			}
			for (Map<String, Object> value : data3) {
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
				dataRow.createCell(2).setCellValue(String.valueOf(value.get("param2")));
				dataRow.createCell(3).setCellValue(String.valueOf(value.get("param3")));
				dataRow.createCell(4).setCellValue(String.valueOf(value.get("param4")));
				dataRow.createCell(5).setCellValue(String.valueOf(value.get("param5")));
			}
			break;
		case 52:
			vo.setSensorNo(deviceNo);
			List<Map<String, Object>> data4 = tbsMapper.tbsDbwy6(vo);
			String[] lxBt4 = new String[] { "数据上传时间", "空气温度", "空气湿度", "大气压力", "风速", "风向", "雨量" };
			for (int a = 0; a < lxBt4.length; a++) {
				// 设置表头信息
				headRow.createCell(a).setCellValue(lxBt4[a]);
			}
			for (Map<String, Object> value : data4) {
				SXSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(String.valueOf(value.get("time")));
				dataRow.createCell(1).setCellValue(String.valueOf(value.get("param1")));
				dataRow.createCell(2).setCellValue(String.valueOf(value.get("param2")));
				dataRow.createCell(3).setCellValue(String.valueOf(value.get("param3")));
				dataRow.createCell(4).setCellValue(String.valueOf(value.get("param4")));
				dataRow.createCell(5).setCellValue(String.valueOf(value.get("param5")));
				dataRow.createCell(6).setCellValue(String.valueOf(value.get("param6")));
			}
			break;
		}
		// 下载导出
		String filename = "设备"+deviceNo+"数据分析"+new SimpleDateFormat("yyyyMMdd").format(new Date());
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		try {
			response.setHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode(filename+".xlsx","UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			// 创建一个输出流
			OutputStream outputStream = response.getOutputStream();
			// 写入数据
			sxssfWorkbook.write(outputStream);
			// 关闭
			outputStream.flush();
			outputStream.close();
			sxssfWorkbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}